<?php
defined('BASEPATH') or exit('No direct script access allowed');
?>

<div class="row">
    <div class="col-sm-12">
        <div class="box">
            <div class="box-header with-border">
                <h2 class="page-header">
                    <i class="fa fa-globe"></i> 所有 SHOPPING 错误
                </h2>
            </div>
            <div class="box-body">
                <div class="row">
                    <div class="col-sm-9">
                        <?php echo form_open(current_url(), array('class' => 'form-inline', 'method' => 'get', 'id' => 'search_goods')); ?>
                        <div class="form-group" style="margin-right:15px;">
                            <select class="form-control" name="code">
                                <option value="">所有错误类型</option>
                                <?php foreach ($codelist as $s): ?>
                                    <option value="<?php echo $s["code"]; ?>" <?php if ($s["code"] == $code) echo " selected"; ?>><?php echo isset($codetype[$s["code"]]) ? $codetype[$s["code"]] : $s["code"]; ?></option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                        <div class="form-group" style="margin-right:15px;">
                            <?php echo form_input($keyword); ?>
                        </div>
                        <?php echo form_button(array('type' => 'submit', 'class' => 'btn btn-sm btn-primary btn-flat', 'content' => "搜索错误")); ?>
                        <?php echo form_close(); ?>
                    </div>
                    <div class="pull-right" style="margin-right:20px;">

                    </div>
                </div>
                <?php if ($loglist): ?>
                    <table class="table table-striped table-hover" style="margin-top:20px;">
                        <thead style="border-top:2px solid #eee;">
                        <tr>
                            <th>ID</th>
                            <th>网站</th>
                            <th>产品名称</th>
                            <th>错误代码</th>
                            <th>描述</th>
                            <th>销量</th>
                            <th>记录时间</th>
                            <th width="150"></th>
                        </tr>
                        </thead>
                        <tbody>
                        <?php foreach ($loglist as $item): ?>
                            <tr shopid="<?php echo $item["id"]; ?>">
                                <td><?php echo $item["skuid"]; ?></td>
                                <td><a href="/backend/sites/product/index/<?php echo $item["siteid"]; ?>"
                                       target="_blank"><?php echo $sindex[$item["siteid"]]; ?></a></td>
                                <td><a href='<?php echo $item["link"]; ?>'
                                       target='_blank'><?php echo $item["title"]; ?></a></td>
                                <td><?php echo $item["code"]; ?></td>
                                <td><?php echo $item["description"]; ?></td>
                                <td><?php echo $item["sellcount"]; ?></td>
                                <td><?php echo $item["addtime"]; ?></td>
                                <td>
                                    <a href="/backend/sites/product/index/<?php echo $item["siteid"]; ?>?keyword=<?php echo urlencode($item["title"]); ?>"
                                       class="btn btn-xs btn-flat btn-info" target="_blank">查找</a>
                                    <button class="btn btn-xs btn-flat btn-danger btn-delete"
                                            itemid="<?php echo $item["id"]; ?>">清除
                                    </button>
                                </td>
                            </tr>

                        <?php endforeach; ?>
                        </tbody>
                    </table>
                <?php endif; ?>
                <?php
                if (empty($loglist)) {
                    echo "<h2 style='text-align:center;padding:50px 0;'>没有查询到数据</h2>";
                }
                ?>

                <?php echo $pager; ?>

                <div class="row">
                    <div class="col-sm-6">
                        <input type="button" class="btn btn-sm btn-danger btn-flat" value="批量删除" id="batchdel">
                    </div>

                    <div class=" pull-right form-inline" style="line-height:30px;margin-top:25px;">
                        <?php echo '共' . $total . '条产品，' . $page . '/' . $pages . '页'; ?>

                        <select id="pagesize" class="form-control input-sm" style="height:22px;">
                            <option value="0">每页数量</option>
                            <option value="50">50</option>
                            <option value="100">100</option>
                            <option value="200">200</option>
                            <option value="500">500</option>
                        </select>

                        <input type="text" id="topage" value="" class="form-control input-sm" placeholder='页码'
                               style="margin:0 5px;height:22px;width:45px;">
                        <?php echo form_button(array('type' => 'submit', 'class' => 'btn btn-xs btn-primary btn-flat', 'id' => 'jumpbtn', 'content' => "跳转")); ?>
                    </div>
                </div>

            </div>
        </div>
    </div>
</div>


<script>


    window.addEventListener('DOMContentLoaded', function () {
        (function ($) {
            $(document).ready(function () {


                // Init iCheck

                var elem_input = $('input');
                if (elem_input.length) {
                    elem_input.iCheck({
                        checkboxClass: 'icheckbox_square-blue',
                        radioClass: 'iradio_square-blue'
                    });
                }

            });


        })(jQuery);
    });


    $(function () {

        function initTableCheckbox() {
            var $thr = $('table thead tr');
            var $checkAllTh = $('<th><input type="checkbox" id="checkAll" name="checkAll" /></th>');
            $thr.prepend($checkAllTh);
            $('#checkAll').on('ifChecked ifUnchecked', function (event) {
                $tbr.find('input').each(function () {
                    $(this).iCheck('toggle');
                    $(this).parent().parent().toggleClass('warning');
                });
            });
            var $tbr = $('table tbody tr');
            var $checkItemTd = $('<td><input type="checkbox" name="checkItem[]" /></td>');
            $tbr.prepend($checkItemTd);

            $tbr.find('input').on('ifChecked ifUnchecked', function (event) {
                $(this).val($(this).parent().parent().parent().attr("shopid"));
                $(this).parent().parent().parent().toggleClass('warning');
            });
            $tbr.click(function () {
                $(this).find('input').iCheck('toggle');
            });
        }

        initTableCheckbox();


        $("button.btn-delete").on("click", function (event) {
            event.preventDefault();
            var cid = $(this).attr("itemid");
            var row = $(this).parent().parent();
            layer.confirm("如果已经修复，需要手动清除记录，请确认？", {title: "确认"}, function (index) {

                shadow = layer.load(1, {
                    shade: [0.1, '#000']
                });
                $.ajax({
                    type: "post",
                    url: "/backend/sites/disapproved/newajaxdel",
                    dataType: "json",
                    data: {"cid": cid},
                    error: function () {
                        layer.close(shadow);
                    },
                    success: function (result) {
                        if (result["status"] == 1) {
                            row.remove();
                        } else {
                            layer.msg(result["msg"]);
                        }
                        layer.close(shadow);
                    },
                });
                layer.close(index);
            });
        });


        $("#batchdel").on("click", function () {

            var checkcount = 0;
            var delids = [];
            $("table tbody").find("input").each(function () {
                if ($(this).prop('checked')) {
                    checkcount = checkcount + 1;
                    delids.push($(this).val());
                }
            });
            if (checkcount == 0) {
                layer.alert('没有选中任何数据');
                return;
            }
            console.log(delids);

            layer.open({
                type: 1,
                skin: 'layui-layer-molv', //样式类名
                closeBtn: 0, //不显示关闭按钮
                anim: 2,
                shadeClose: false, //开启遮罩关闭
                content: "<div style='padding:20px 20px;' id='tipmsg'>共有" + delids.length + "条产品等待删除,请勿关闭窗口</div>"
            });

            function delproduct(delids) {
                let defer = $.Deferred();
                let dcount = 0;
                $.each(delids, function (index, val) {
                    $.ajax({
                        url: "/backend/sites/disapproved/ajaxdel",
                        type: "post",
                        data: {"cid": val},
                        dataType: "json",
                        success: function (result) {
                            dcount = dcount + 1;
                            if (result.status == 0) {
                                layer.msg(result.msg);
                            } else {
                                $("#tipmsg").text('已经删除产品 ' + dcount + '/' + delids.length + ',请勿关闭窗口');
                            }
                            if (delids.length == dcount) {
                                defer.resolve();
                            }
                        }
                    });
                });
                return defer;
            }

            delproduct(delids).then(function () {
                layer.closeAll();
                layer.msg("删除成功");
                setTimeout(() => {
                    location.href = location.href;
                }, 2000);
            });
        });

    });


    $("select#pagesize").change(function () {
        pagesize = $("select#pagesize option:selected").val();
        if (pagesize > 0) {
            var url = window.location.href;
            var newUrl = changeURLArg(url, "pagesize", pagesize);
            window.location.href = newUrl;
        }
    });


    function changeURLArg(url, arg, arg_val) {
        var pattern = arg + '=([^&]*)';
        var replaceText = arg + '=' + arg_val;
        if (url.match(pattern)) {
            var tmp = '/(' + arg + '=)([^&]*)/gi';
            tmp = url.replace(eval(tmp), replaceText);
            return tmp;
        } else {
            if (url.match('[\?]')) {
                return url + '&' + replaceText;
            } else {
                return url + '?' + replaceText;
            }
        }
        return url + '\n' + arg + '\n' + arg_val;
    }
</script>
